﻿using ASmile.CodeFactory.DBModel;
using ASmile.Com.Da;
using ASmile.Extend;
using ASmile.ORM.Cores;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASmile.CodeFactory.DaBLL
{
    class ConfigBLL : BLLCrudBase<TConfig>
    {
        protected override void SetQuery(QueryCondition<TConfig> query, ArgPageData arg)
        {
            if (arg.SearchVal.IsNotEmpty())
            {
                string likeStr = arg.SearchValLike;
                query.WhereAnd(s =>
                    s.WEx_Like(s.NameSpace, likeStr) ||
                    s.WEx_Like(s.Caption, likeStr));
            }
        }

        protected override ApiResult<string> VerifyModel(TConfig model, bool isAdd)
        {
            if (model.Caption.IsEmpty())
            {
                return RetErr<string>("Caption", "标题不能为空");
            }
            if (model.NameSpace.IsEmpty())
            {
                return RetErr<string>("NameSpace", "命名空间不能为空");
            }
            if (model.SavePath.IsEmpty())
            {
                return RetErr<string>("SavePath", "保存路径不能为空");
            }
            if (model.DBType.IsEmpty())
            {
                return RetErr<string>("SavePath", "数据库类型不能为空");
            }
            if (model.ConnString.IsEmpty())
            {
                return RetErr<string>("SavePath", "连接字符串不能为空");
            }
            if (isAdd)
            {
                model.Id = NewId();
                return RetOK(model.Id, "新增成功");
            }
            else
            {
                return RetOK("","更新成功");
            }
        }
    }
}
